/*-----------------------------------粘贴图片------------------------------------*/
function domPaste(){
	document.querySelector('.messContent').addEventListener('paste',function(e){
		 var cbd = e.clipboardData;
		    var ua = window.navigator.userAgent;
		    // 如果是 Safari 直接 return
		    if ( !(e.clipboardData && e.clipboardData.items) ) {
		        return ;
		    }
		    // Mac平台下Chrome49版本以下 复制Finder中的文件的Bug Hack掉
		    if(cbd.items && cbd.items.length === 2 && cbd.items[0].kind === "string" && cbd.items[1].kind === "file" &&
		        cbd.types && cbd.types.length === 2 && cbd.types[0] === "text/plain" && cbd.types[1] === "Files" &&
		        ua.match(/Macintosh/i) && Number(ua.match(/Chrome\/(\d{2})/i)[1]) < 49){
		        return;
		    }

		    for(var i = 0; i < cbd.items.length; i++) {
		        var item = cbd.items[i];
		        if(item.kind == "file"){
		            var blob = item.getAsFile();
		            if (blob.size === 0) {
		                return;
		            }
		            // blob 就是从剪切板获得的文件 可以进行上传或其他操作
					var data = new FormData();
					data.append('pic', blob);
					/*data.append('userId',1);*/
					$.ajax({
					    url: '/ps/task/addPicjson',
					    type: 'POST',
					    dataType:'json',
					    cache: false,
					    data: {
					    	data,
					    	'userId':1
					    },
					    processData: false,
					    contentType: false,
					    success:function(res){
					    	var wrap = document.querySelector('.messContent');
						    var file = res.data.toString();
						    var img = document.createElement("img");
					        img.src = file;
					        if(Boolean(res)){
					        	$('#upImg').val(file);
					        }
							wrap.appendChild(img);
					    },error:function(){
					    	
					    }
					})
		        }
		    }
		}, false);
}
/*domPaste();*/
/*-----------------------------------修改状态-------------------------------------*/
var sort = '';
$(document).on('click','.statusWrap span',function(){
	var status = $(this).attr('status');
	var userId = $('input[name=userId]').val();
	var _this = $(this);
	$.ajax({
	    url: '/ps/task/installListMore.htm',
	    type: 'GET',
	    dataType:'html',
	    data:{
	    	'status':status,
	    	'userId':userId,
	    	'sort':sort
	    },
	    success:function(res){
	    	console.log(res);
	    	domPaste();
	    	$('.messageContent').html(res);
	    	_this.addClass('statusCheck').siblings().removeClass('statusCheck');
	    	$('input[name=sortTime]').val(status);
	    	 var pageCount=$(".pageCount").val();
	    	    $(document).find(".tcdPageCode").createPage({
	    	        pageCount:pageCount,
	    	        current:$('.pageNo').val(),
	    	        backFn:function(p){
//	    	            var companyNameVar = $(".companyName").val();
//	    				if(companyNameVar!=null){
//	    					companyNameVar = encodeURI(encodeURI(companyNameVar));
//	    				}
	    	        	window.location.href="/ps/task/install.htm?pageNo="+p+"&userId="+$("#userId").val()+"&status="+$("#status").val()
	    	        	+"&sort="+$("#sort").val();
	    	        }
	    	    });
	    	    if(status == 1){
		    		 $('.rightNum').prev().html('安装中');
		    	 }else if(status == 0){
		    		 $('.rightNum').prev().html('未接受');
		    	 }else{
		    		 $('.rightNum').prev().html('全部'); 
		    	 }
	    	    $('.rightNum').html($('.count').val());
	    },error:function(){
	    	
	    }
	})
})

/*----------------------------------修改排序状态---------------------------------*/
$(document).on('click','.timeSort',function(){
	//status:0为down,status:1为up
	var status = $('input[name=sortTime]').val();
	var userId = $('input[name=userId]').val();
	var _this = $(this);
	if($(this).attr('status') == 0){
		sort = $(this).attr('up');
		$.ajax({
		    url: '/ps/task/installListMore.htm',
		    type: 'GET',
		    dataType:'html',
		    data:{
		    	'status':status,
		    	'userId':userId,
		    	'sort':sort
		    },
		    success:function(res){
		    	domPaste();
		    	$('.messageContent').html(res);
		    	_this.find('i').css('background','url('+_this.find('i').attr('data-y')+')');
		    	_this.attr('status',1);
		    	 var pageCount=$(".pageCount").val();
		    	    $(document).find(".tcdPageCode").createPage({
		    	        pageCount:pageCount,
		    	        current:$('.pageNo').val(),
		    	        backFn:function(p){
//		    	            var companyNameVar = $(".companyName").val();
//		    				if(companyNameVar!=null){
//		    					companyNameVar = encodeURI(encodeURI(companyNameVar));
//		    				}
		    	        	window.location.href="/ps/task/install.htm?pageNo="+p+"&userId="+$("#userId").val()+"&status="+$("#status").val()
		    	        	+"&sort="+$("#sort").val();
		    	        }
		    	    });
		    	    if(status == 1){
			    		 $('.rightNum').prev().html('安装中');
			    	 }else if(status == 0){
			    		 $('.rightNum').prev().html('未接受');
			    	 }else{
			    		 $('.rightNum').prev().html('全部'); 
			    	 }
		    	    $('.rightNum').html($('.count').val());
		    },error:function(){
		    	
		    }
		})
	}else{
		var sort = $(this).attr('down');
		$.ajax({
		    url: '/ps/task/installListMore.htm',
		    type: 'GET',
		    dataType:'html',
		    data:{
		    	'status':status,
		    	'userId':userId,
		    	'sort':sort
		    },
		    success:function(res){
		    	domPaste();
		    	$('.messageContent').html(res);
		    	_this.find('i').css('background','url('+_this.find('i').attr('data-x')+')');
		    	_this.attr('status',0);
		    	 var pageCount=$(".pageCount").val();
		    	    $(document).find(".tcdPageCode").createPage({
		    	        pageCount:pageCount,
		    	        current:$('.pageNo').val(),
		    	        backFn:function(p){
//		    	            var companyNameVar = $(".companyName").val();
//		    				if(companyNameVar!=null){
//		    					companyNameVar = encodeURI(encodeURI(companyNameVar));
//		    				}
		    	        	window.location.href="/ps/task/install.htm?pageNo="+p+"&userId="+$("#userId").val()+"&status="+$("#status").val()
		    	        	+"&sort="+$("#sort").val();
		    	        }
		    	    });
		    	    if(status == 1){
			    		 $('.rightNum').prev().html('安装中');
			    	 }else if(status == 0){
			    		 $('.rightNum').prev().html('未接受');
			    	 }else{
			    		 $('.rightNum').prev().html('全部'); 
			    	 }
		    	    $('.rightNum').html($('.count').val());
		    },error:function(){
		    	
		    }
		})
		
	}
})

/*------------------------------图片点击查看原图------------------------------*/
//点击图片查看原图
$(document).on('click','.initImg',function(){
	var imgs = new Image(); 
	$(imgs).attr('src',$(this).attr('src')); 
	$('.showInitImg').html(imgs);
	$('.showInitImg').show();
	$('.showInitImgBg').show();
})
//点击背景隐藏原图
$(document).on('click','.showInitImgBg',function(){
	$('.showInitImg').hide();
	$('.showInitImgBg').hide();
})

/*-----------------------------获取当前时间------------------------------------*/
function addNum(num){
	if(num < 10){
		num = '0'+num;
	}else{
		num = num;
	}
	return num;
}
window.onload = function(){
	
	var datejs= $('#datejs').val();
	getAjax();
	/* setInterval('getAjax(date)',5000);*/
	 function getAjax(){
			$.ajax({
			    url: '/ps/task/sendTaskNum',
			    type: 'GET',
			    dataType:'json',
			    data: {
			    	'timeStr':datejs,
			    	'userId':1
			    },
			    success:function(res){
			    	if(res.data > 0){
		    			if(! ('Notification' in window) ){
		    				alert('Sorry bro, your browser is not good enough to display notification');
		    				return;
		    			}else{
		    				Notification.requestPermission(function(permission){
			    				var notification = new Notification("您有新消息",{body:res.data,icon:'http://image1.zhiwangvip.com/static/images/ps/jiedan.png',dir:'auto'});
			    				setTimeout(function() {
			                        notification.close();
			                    }, 5000);
			    				notification.onclick = function(){
			    					notification.close();
				    				var href = window.location.href;
				    				window.close();
				                	window.open(href);
				    			}
			    			});
		    			}	
			    	}
			    },error:function(){
			    	
			    }
		})
		setTimeout(getAjax,5000);
	}
}
/*-------------------------------接受任务------------------------------*/
$(document).on('click','.accept',function(){
	var _this = $(this);
	$.ajax({
	    url: '/ps/task/receiveTaskJson',
	    type: 'GET',
	    dataType:'json',
	    data:{
	    	'installerId':$(this).parent().find('input[name=installerId]').val(),
	    	'psTaskId':$(this).parent().find('input[name=psTaskId]').val()
	    },
	    success:function(res){
	    	if(res['code'] == 200){
	    		if(Boolean($(_this).parent().parent().find('.infoWrap').find('.title').val())){
	    			$(_this).parent().parent().find('.infoWrap').find('p').html($(_this).parent().parent().find('.infoWrap').find('.title').val());
	    		}
	    		_this.html('已接受');
	    		_this.addClass('accepted').removeClass('accept');
	    	}else if(res['code'] == -500){
	    		alert("任务已经被领取");
	    	}
	    },error:function(){
	    	
	    }
	})
})


